﻿using Ross.Service;
using Ross.Service.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Web.Http;
using System.Web.Http.Results;
using System.Web.Http.Controllers;
using Ross.EntityFramework.Model;
using Ross.EntityFramework;
using System.Web;
using System.IO;
using System.Collections.Specialized;
using System.Text;
using System.Configuration;
using System.Net.Http.Headers;

namespace Ross.Traning.Controllers
{
    [SimpleAuthenticationFilter]
    public class CoursewareController : BaseApiController
    {
        [HttpGet]
        public JsonResult<ResultDto<List<Service.Dto.Courseware>>> GetList(long courseId)
        {
            ResultDto<List<Service.Dto.Courseware>> result = new ResultDto<List<Service.Dto.Courseware>>();
            using (CoursewareService serv = new CoursewareService(dbContextStr))
            {
                result = serv.GetAllList(courseId);
            }
            return Json(result);
        }
        [HttpPost]
        public ResultDto<string[]> UploadFile(long CourseID, string Author)
        {
            ResultDto<string[]> result = new ResultDto<string[]>();
            try
            {
                string absPath = HttpContext.Current.Server.MapPath(uploadFilePath);
                if (!Directory.Exists(absPath))
                {
                    Directory.CreateDirectory(absPath);
                }
                HttpRequest request = System.Web.HttpContext.Current.Request;
                HttpFileCollection fileCollection = request.Files;
                string[] Paths = new string[fileCollection.Count];
                if (fileCollection.Count > 0)
                {
                    for (int i=0;i<fileCollection.Count;i++) {
                        HttpPostedFile httpPostedFile = fileCollection[i];
                        string fileExtension = Path.GetExtension(httpPostedFile.FileName);// 文件扩展名
                        string OKExtension = ConfigurationManager.AppSettings["UploadFileType"];
                        if (OKExtension.IndexOf(fileExtension.Substring(1)) < 0)
                        {
                            result.status = 500;
                            Paths[i] = null;
                            result.datas = Paths;
                            continue;
                        }
                        else
                        {
                            string fileName = Guid.NewGuid().ToString() + fileExtension;// 文件名称
                            string filePath = absPath + httpPostedFile.FileName;// 上传路径
                            string sfile = absPath + fileName + fileExtension;
                            while (File.Exists(filePath))
                            {
                                fileName = Guid.NewGuid().ToString() + fileExtension;
                                filePath = absPath + fileName;
                            }
                            httpPostedFile.SaveAs(filePath);
                            Service.Dto.Courseware cw = new Service.Dto.Courseware();
                            cw.FileURL = uploadFilePath + httpPostedFile.FileName;
                            cw.Title = Path.GetFileNameWithoutExtension(httpPostedFile.FileName);
                            cw.DocType = fileExtension;
                            cw.CourseID = CourseID;
                            cw.Author = Author;
                            cw.CreateTime = DateTime.Now;
                            using (CoursewareService serv = new CoursewareService(dbContextStr))
                            {
                                serv.Create(cw);
                            }                 
                            Paths[i] = uploadFilePath + httpPostedFile.FileName;

                        }
                    }
                    result.status = 200;
                    result.datas =Paths;
                    result.message = "上传成功！";


                }
            }
            catch (Exception ex)
            {
                result.status = 500;
                result.datas = null;
                result.message = ex.Message;
            }
            return result;
        }

        [HttpPost]
        public JsonResult<ResultDto<bool>> Delete(int id)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (CoursewareService serv = new CoursewareService(dbContextStr))
            {
                result = serv.Delete(id);
            }
            return Json(result);
        }
    }


}
